package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.Dishservice;
import com.sky.service.impl.Dishservicelml;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private Dishservicelml dishservicelml;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public Result insertdish(@RequestBody DishDTO dto) {
        int count = dishservicelml.insertdish(dto);
        if (count == 1) {
            return Result.success();
        } else {
            return Result.error("插入失败");
        }
    }

    @GetMapping("/page")
    public Result selectwithpage(
            @RequestParam(value = "page", required = true)
            Integer page,
            @RequestParam(value = "pageSize", required = true)
            Integer pageSize,
            @RequestParam(value = "name", required = false)
            String name,
            @RequestParam(value = "categoryId", required = false)
            Integer categoryId,
            @RequestParam(value = "pagesize", required = false)
            Integer status
    ) {
        DishPageQueryDTO queryDTO = DishPageQueryDTO.builder()
                .page(page)
                .pageSize(pageSize)
                .name(name)
                .categoryId(categoryId)
                .status(status)
                .build();
        log.info("DishPageQueryDTO{}", queryDTO);
        //开启分页查询
        PageHelper.startPage(page, pageSize);
        Page<Dish> p = dishservicelml.selectwithpage(queryDTO);
        if (p != null && p.size() > 0) {
            PageResult pageResult = new PageResult(p.getTotal(), p);
            return Result.success(pageResult);
        }
        return Result.error("分页查询失败");
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public Result deletebyids(@RequestParam List<Long> ids) {
        String message = dishservicelml.deletebyids(ids);
        if (message == "删除成功") {
            return Result.success(message);
        }
        else if (message == "不可删除") {
            return Result.error(message);
        } else
            return Result.error(message);
    }

    @GetMapping("/{id}")
    public Result selectDishDTO(@PathVariable("id") Long id) {
        DishDTO dishDTO = dishservicelml.selectDish(id);
        if (dishDTO != null)
            return Result.success(dishDTO);
        else
            return Result.error("查询失败");
    }
    @PutMapping
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public Result updateDish(@RequestBody DishDTO dto)
    {
        Boolean flag = dishservicelml.updatedish(dto);
        if(flag)
        {
            return Result.success();
        }
        else
            return Result.error("更新失败");
    }
}
